<template>
<div>
  <bread ref="bread"></bread>
  <el-card>
   <el-button type="primary" @click="addRolesDialog=true">添加角色</el-button>
    <el-table :data="rolesList" border stripe>
      <el-table-column type="expand">
       <template v-slot="scope">
        <el-row v-for="item1 in scope.row.children" :key="item1.id" class="vcenter" style="border-bottom: 1px solid #eeeeee">
          <el-col :span="5">
            <el-tag closable @close="removeRightByID(scope.row,item1.id)">{{ item1.authName }}</el-tag>
            <i class="el-icon-caret-right"></i>
          </el-col>
          <el-col :span="19" >
           <el-row v-for="item2 in item1.children" :key="item2.id" class="vcenter" style="border-bottom: 1px solid #eeeeee">
             <el-col :span="6">
               <el-tag type="success" closable @close="removeRightByID(scope.row,item2.id)" >
                 {{ item2.authName}}
               </el-tag>
               <i class="el-icon-caret-right"></i>
             </el-col>
             <el-col :span="18">
               <el-tag v-for="item3 in item2.children" :key="item3.id" type="warning" closable @close="removeRightByID(scope.row,item3.id)">
                 {{ item3.authName}}
                 <i class="el-icon-caret-right"></i>
               </el-tag>
             </el-col>
           </el-row>
          </el-col>
        </el-row>
       </template>
      </el-table-column>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作" width="300px">
        <template v-slot="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editRoles(scope.row.id)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRoles(scope.row.id)">删除</el-button>
        <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightsDialog(scope.row)">分配</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
<!--  添加角色-->
  <el-dialog
      title="添加角色"
      :visible.sync="addRolesDialog"
      width="50%" @close="addRolesClear">
      <el-form :rules="rolesRules" ref="addRolesRef" :model="addRolesList">
      <el-form-item label="角色名"  prop="roleName">
        <el-input v-model="addRolesList.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="addRolesList.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="addRolesDialog = false">取 消</el-button>
    <el-button type="primary" @click="addRoles">确 定</el-button>
    </span>
  </el-dialog>
<!--  编辑角色信息-->
  <el-dialog
      title="编辑角色信息"
      :visible.sync="editRolesDialog"
      width="50%">
    <el-form :rules="rolesRules" ref="addEditRolesRef" :model="editRolesList">
      <el-form-item label="角色名" prop="roleName">
        <el-input v-model="editRolesList.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="editRolesList.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="editRolesDialog = false">取 消</el-button>
    <el-button type="primary" @click="addEditRoles">确 定</el-button>
    </span>
  </el-dialog>
<!--  分配权限的dialog-->
  <el-dialog
      title="分配权限"
      :visible.sync="setRolesDialog"
      width="50%">
    <el-tree ref="treeRef" :data="rightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys"></el-tree>
    <span slot="footer" class="dialog-footer">
    <el-button @click="setRolesDialog = false">取 消</el-button>
    <el-button type="primary" @click="addRightsList">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
import bread from "@/components/bread";
export default {
  name: "roles",
  components: {bread},
  data(){
    return {
      rolesList:[],
      addRolesDialog:false,
      editRolesDialog:false,
      setRolesDialog:false,
      editRolesList:{},
      breadList:[],
      roleId:'',
      treeProps:{
        children:'children',
        label:'authName'
      },
      addRolesList:{
        roleName:'',
        roleDesc:''
      },
      rolesRules:{
        roleName: [
          {required: true ,message:'请输入角色名' ,trigger: 'blur' },
        ],
      },
      rightsList:[],
      defKeys:[105,116]
    }
  },
  async created() {
    this.getRolesList()
    const { data: res } = await this.$http.get('menus')
    this.breadList.push(res.data[1].authName)
    this.breadList.push(res.data[1].children[0].authName)
    this.$refs.bread.getBreadList(this.breadList)
  },
  methods: {
    // 获取角色列表
    async getRolesList() {
      const {data: res} = await this.$http.get('roles')
      this.rolesList = res.data
    },
    //添加角色
    async addRoles(){
    const { data:res} = await this.$http.post('roles',{ roleName:this.addRolesList.roleName ,roleDesc:this.addRolesList.roleDesc})
      if (res.meta.status===201){
        this.$message.success(res.meta.msg)
      }
      else {
        this.$message.error(res.meta.msg)
      }
      this.addRolesDialog=false
      await this.getRolesList()
    },
    //角色对话框关闭时清理
    addRolesClear(){
      this.$refs.addRolesRef.resetFields()
    },
    //展示角色列表
    async editRoles(id) {
      this.editRolesDialog = true
      const {data: res} = await this.$http.get(`roles/${id}`)
      this.editRolesList = res.data
      console.log(this.editRolesList)
    },
    //提交修改的角色列表
    async addEditRoles() {
      const {data: res} = await this.$http.put('roles/' + this.editRolesList.roleId, {
        roleName: this.editRolesList.roleName,
        roleDesc: this.editRolesList.roleDesc
      })
      if (res.meta.status === 200) {
        this.$message.success('修改成功')
      } else {
        this.$message.error('修改失败')
      }
      this.editRolesDialog = false
      await this.getRolesList()
    },
    //删除角色
    async deleteRoles(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        const { data:res } =await this.$http.delete(`roles/${id}`)
        if (res.meta.status===200){
          this.$message.success(res.meta.msg)

        }
        else {
          this.$message.error(res.meta.msg)
        }
        await this.getRolesList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //删除权限
    removeRightByID(role,rightId){
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        const { data:res } =await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status===200){
          this.$message.success(res.meta.msg)
          role.children =res.data
        }
        else {
          this.$message.error(res.meta.msg)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //展示分配权限的对话框
    async showSetRightsDialog(role){
      this.roleId=role.id
       this.defKeys=[]
       const { data:res} = await this.$http.get(`rights/tree`)
      if (res.meta.status==200){
        this.$message.success(res.meta.msg)
        this.rightsList=res.data
        this.getKeys(role,this.defKeys)
      }
      else {
        this.$message.error(res.meta.msg)

      }
      this.setRolesDialog=true
    },
    //获取已勾选权限
    getKeys(node ,arr){
      if (!node.children){
        return arr.push(node.id)
      }
      node.children.forEach(item =>{
        this.getKeys(item,arr)
      })
    },
    //添加权限
    async addRightsList(){
      const keys=[
          ...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const idStr =keys.join(',')
      console.log(idStr)
      const {data:res}=await this.$http.post(`roles/${this.roleId}/rights`,{ rids:idStr })
      if (res.meta.status==200){
        this.$message.success(res.meta.msg)
        this.getRolesList()
      }
      else {
        this.$message.error(res.meta.msg)
      }
      this.setRolesDialog=false
    }
  }
}
</script>

<style scoped>
.el-tag{
  margin: 10px;
}
.bdtop{
  border-top: 1px solid #eee;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>
